package com.example.hanqinshan.gifttalk.home.channel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.hanqinshan.gifttalk.R;
import com.example.hanqinshan.gifttalk.entity.BaseEntity;
import com.example.hanqinshan.gifttalk.entity.ChannelItemData;
import com.example.hanqinshan.gifttalk.http.HttpManager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Risky57 on 2017/6/27.
 */

public class ChannelFragment extends Fragment {

    private static final String KEY_ID = "ID";
    private ChannelAdapter mAdapter;

    public static ChannelFragment newInstance (int id) {

        Bundle args = new Bundle();
        args.putInt(KEY_ID, id);
        ChannelFragment fragment = new ChannelFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private int id;
    private ListView mListView;

    @Nullable
    @Override
    public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_channel, container, false);
    }

    @Override
    public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mListView = (ListView) view.findViewById(R.id.list_view);
    }

    @Override
    public void onActivityCreated (@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        id = getArguments().getInt(KEY_ID);
        if (id == 101){
            // 添加头布局
        }
        mAdapter = new ChannelAdapter(getActivity());
        mListView.setAdapter(mAdapter);
        Call<BaseEntity<ChannelItemData>> itemsCall = HttpManager.getApi().getChannelItems(id);
        itemsCall.enqueue(new Callback<BaseEntity<ChannelItemData>>() {
            @Override
            public void onResponse (Call<BaseEntity<ChannelItemData>> call, Response<BaseEntity<ChannelItemData>> response) {
                mAdapter.addAll(response.body().getData().getItems());
            }

            @Override
            public void onFailure (Call<BaseEntity<ChannelItemData>> call, Throwable t) {

            }
        });
    }

    private View createBannerView(){

        return null;
    }

    private View createItemView(){

        return null;
    }
}
